/* @(#)GSApepInfoProcessListener.java   
 *
 * Project: GS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Jenny Li    2006-3-4     新增
 *   Jenny Li    2006-6-23   修改processFinishOrder,新增送货单号字段
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.integration;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hitrust.gs.bean.GSOrderDetailBean;
import com.hitrust.gs.dao.GSOrderDetailDAO;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.util.ParameterFactory;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.exception.GSException;


/**
 * 
 */
public class GSApepInfoProcess  {
    /**
     * 待处理内容
     */
    private String iContent = "";
    /**
     * 取得存储从APEP传入信息的容器
     */
    private ArrayList iList;
    /**
     * 
     * @param aContent
     */
    public GSApepInfoProcess(){
//        start();
    }
    /**
     * @deprecated
     *
     */
    public void run(){
        try{
            while(GSBaseConstants.SOCKET_SERVICE_FLAG.equals("1")){
                //sleep(30*1000);
                iList = GSServerListener.APEP_TO_BUS_CONTENTS;
                try {
                    if(iList != null && iList.size() > 0){
                        GSLogWriter.debug(this,"list size=" + iList.size());                    
                        iContent = get1stContent(iList);
                        while (iContent != null && !iContent.equalsIgnoreCase("&END")){                        
                            try{
                                //处理接收到的信息
                                String tResult = process(iContent);
                                GSLogWriter.debug(this,"Result=" + tResult);                                
                            }catch(Exception e){
                                GSLogWriter.error(this,e.toString());                            

                            }
                            iList.remove(0);
                            iContent = get1stContent(iList);
                        }
                    }
                }
                catch (Exception e) {                   
                    e.printStackTrace();
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    private String get1stContent(ArrayList alst){
        String rt = null;
        if (alst != null){
            while (rt == null && alst.size()>0){        
                rt = (String)alst.get(0);
                if (rt == null)
                    alst.remove(0);
            }
        }
        return rt;
    }
    /**
     * 处理交易平台发送的订单明细信息
     * @return              处理成功返回：1，失败：0
     * @throws GSException
     */
    public String process(String value) throws GSException{
        this.iContent = value;
        String tRs = "";
        
        try{
            if(iContent != null && !iContent.equals("")){
                
                String tAction = iContent.substring(7,iContent.indexOf("&"));
				
				//add by Jackey start
				//测试APEP发送信息接口是否有效--（未开通 2007-05-22）
				if(tAction.equalsIgnoreCase("ApepToGsTest")){
					tRs = "1";
				}
				// add by Jackey end
				
				//处理订单交货				
				else if(tAction.equalsIgnoreCase("SendOrder")){
                    tRs = processSendOrder();
                }else if(tAction.equalsIgnoreCase("RefuseOrder")){//处理订单拒绝
                    tRs = processRefuseOrder();
                }else if(tAction.equalsIgnoreCase("FinishOrder")){//处理交货确认
                    tRs = processFinishOrder();
	            }else if(tAction.equalsIgnoreCase("DisposeCancelOrder")){//处理取消审批
	                tRs = processDisposeCancelOrder();
	            }else if(tAction.equalsIgnoreCase("DeposeOrder")){//处理订单作废 by Sindy 2007-10-29
					tRs = processDeposeOrder();
	            }
	            
                else {
                    throw new GSException(GSException.E10055,GSException.E10055_MSG);
                }
            }else{
                GSLogWriter.debug(this,"传入内容为空！");
            }
        }catch(GSException e){
            tRs = "0";
            throw e;
        }catch(Exception e){
            tRs = "0";
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E00001,GSException.E00001_MSG);
        }
        return tRs;
    }
    /**
     * 处理交易平台发送的订单明细送货确认信息
     * @return
     * @throws GSException
     */
    private String processSendOrder() throws GSException{
        String tRs = "";
        Connection tConn = null;
        try{
            String tStr[] = iContent.split("&",4);
            int tCount = 0;
            //检查订单明细汇总信息格式
            if(tStr[1].substring(0,tStr[1].indexOf("=")).equalsIgnoreCase("Count")){
                GSLogWriter.debug("count=" + tStr[1].substring(tStr[1].indexOf("=") + 1));
                tCount = Integer.parseInt(tStr[1].substring(tStr[1].indexOf("=") + 1));
            }else{
                GSLogWriter.error(this,"传入内容不包含    送货确认数量字段");
                throw new GSException(GSException.E10052,GSException.E10052_MSG);
            }
            double tTotal = 0;
            if(tStr[2].substring(0,tStr[2].indexOf("=")).equals("Total")){
                GSLogWriter.debug("count=" + tStr[2].substring(tStr[2].indexOf("=") + 1));
                tTotal = Double.parseDouble(tStr[2].substring(tStr[2].indexOf("=") + 1));
            }else{
                GSLogWriter.error(this,"传入内容不包含   产品配送数量汇总字段");
                throw new GSException(GSException.E10052,GSException.E10052_MSG);
            }
            String[] temp = tStr[3].split("&");
            if(temp.length % tCount != 0){
                GSLogWriter.error(this,"传入内容明细字段有缺失或多余");
                throw new GSException(GSException.E10052,GSException.E10052_MSG);
            }
            int n = temp.length / tCount;
            if(n != 10){
                GSLogWriter.error(this,"传入内容明细字段有缺失或多余");
                throw new GSException(GSException.E10052,GSException.E10052_MSG);
            }
            String[][] tDetail = new String[tCount][10];
            double tSendCount = 0;
            int tOrderCount = 0;
            //检查传入明细内容格式是否正确
            for(int i=0; i<tCount;i++){
                int j= i*10;
                if(!temp[j].substring(0,temp[j].indexOf("_")).equalsIgnoreCase("CusOrderItemCode")){
                    GSLogWriter.error(this,"集中采购系统订单明细ID错误");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }else{
                    tDetail[i][0] = temp[j].substring(temp[j].indexOf("=") + 1);
                    GSLogWriter.debug("ProductID=" + tDetail[i][0]);
                    tOrderCount = tOrderCount + 1;
                }
//                if(!temp[j+1].substring(0,temp[j + 1].indexOf("_")).equalsIgnoreCase("CusItemCode")){
//                    GSLogWriter.error(this,"产品编号错误");
//                    throw new GSException(GSException.BME10052,GSException.BME10052_MSG);
//                }
                if(!temp[j+1].substring(0,temp[j + 1].indexOf("_")).equalsIgnoreCase("ProductID")){
                    GSLogWriter.error(this,"产品编号错误");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }else{
                    GSLogWriter.debug("ProductID=" + temp[j + 1].substring(temp[j + 1].indexOf("=") + 1));
                }
                if(!temp[j+2].substring(0,temp[j + 2].indexOf("_")).equalsIgnoreCase("SendCode")){
                    GSLogWriter.error(this,"送货单号错误");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }else{
                    tDetail[i][1] = temp[j + 2].substring(temp[j + 2].indexOf("=") + 1);
                    GSLogWriter.debug("SendCode=" + temp[j + 2].substring(temp[j + 2].indexOf("=") + 1));
                }
                if(!temp[j+3].substring(0,temp[j + 3].indexOf("_")).equalsIgnoreCase("SendQty")){
                    GSLogWriter.error(this,"配送数量错误");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }else{
                    tDetail[i][2] = temp[j + 3].substring(temp[j+3].indexOf("=") + 1);
                    GSLogWriter.debug("SendQty=" + tDetail[i][2]);
                    tSendCount = tSendCount + Double.parseDouble(tDetail[i][2]);
                }
                if(!temp[j+4].substring(0,temp[j + 4].indexOf("_")).equals("SendTime")){
                    GSLogWriter.error(this,"送货日期和时间错误");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }else{
                    GSLogWriter.debug("SendTime=" + temp[j + 4].substring(temp[j + 4].indexOf("=") + 1));
                    tDetail[i][5] = temp[j + 4].substring(temp[j + 4].indexOf("=") + 1);
                }
                if(!temp[j+5].substring(0,temp[j + 5].indexOf("_")).equalsIgnoreCase("SendPerson")){
                    GSLogWriter.error(this,"   送货联系人");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }else{
                    GSLogWriter.debug("SendPerson=" + temp[j + 5].substring(temp[j + 5].indexOf("=") + 1));
                    tDetail[i][3] = temp[j + 5].substring(temp[j + 5].indexOf("=") + 1);
                }
                if(!temp[j+6].substring(0,temp[j + 6].indexOf("_")).equalsIgnoreCase("SendTel")){
                    GSLogWriter.error(this,"   送货联系电话");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }else{
                    GSLogWriter.debug("SendTel=" + temp[j + 6].substring(temp[j + 6].indexOf("=") + 1));
                    tDetail[i][4] = temp[j + 6].substring(temp[j + 6].indexOf("=") + 1);
                }
                if(!temp[j+7].substring(0,temp[j + 7].indexOf("_")).equalsIgnoreCase("SendCreateTime")){
                    GSLogWriter.error(this,"   送货信息填写日期和时间");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }else{
                    GSLogWriter.debug("SendCreateTime=" + temp[j + 7].substring(temp[j + 7].indexOf("=") + 1));
//                    tDetail[i][5] = temp[j + 7].substring(temp[j + 7].indexOf("=") + 1);
                }
                if(!temp[j+8].substring(0,temp[j + 8].indexOf("_")).equalsIgnoreCase("SendRemark")){
                    GSLogWriter.error(this,"   送货说明");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }else{
                    GSLogWriter.debug("SendRemark=" + temp[j + 8].substring(temp[j + 8].indexOf("=") + 1));
                    tDetail[i][6] = temp[j + 8].substring(temp[j + 8].indexOf("=") + 1);
                }
                if(!temp[j+9].substring(0,temp[j + 9].indexOf("_")).equalsIgnoreCase("CusCode")){
                    GSLogWriter.error(this,"   巴士物资编号");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }
                GSLogWriter.debug("CusCode=" + temp[j + 9].substring(temp[j + 9].indexOf("=") + 1));
                
            }
            if(tCount != tOrderCount){
                GSLogWriter.error(this,"订单明细送货确认数量和订单明细实际的数量不相符");
                throw new GSException(GSException.E10052,GSException.E10052_MSG);
            }
            GSOrderDetailDAO tOrderDetailDAO = new GSOrderDetailDAO();
            tConn = tOrderDetailDAO.getConnetion();
            tConn.setAutoCommit(false);
            double tDetailCount = 0;
            ArrayList tList = new ArrayList();
            for(int i=0; i<tDetail.length; i++){
                //根据传入的每条订单明细送货确认信息的订单编号查询订单明细
                GSOrderDetailBean tOrderDetailBean = tOrderDetailDAO.queryOrderDetailByDetailId(tConn,tDetail[i][0]);
                if(tOrderDetailBean != null){
                    tDetailCount = tDetailCount + tOrderDetailBean.getGs_order_count();
                    GSLogWriter.debug("orderbean gs_order_count=" + tOrderDetailBean.getGs_order_count() + " : detail gs_order_count=" + Double.parseDouble(tDetail[i][2]));
                    if(tOrderDetailBean.getGs_order_count() != Double.parseDouble(tDetail[i][2])){
                        GSLogWriter.error(this,"订货数量不等于配送数量！");
//                        throw new GSException(GSException.BME10052,GSException.BME10052_MSG);
                    }
//                    else{
                        tOrderDetailBean.setGs_order_detail_status(GSBaseConstants.ORDER_ORDER_CONFIRM);
                        tOrderDetailBean.setGs_deliver_goods_counts(Double.parseDouble(tDetail[i][2]));
                        tOrderDetailBean.setGs_deliver_goods_id(tDetail[i][1]);
                        tOrderDetailBean.setGs_deliveryman(tDetail[i][3]);
                        tOrderDetailBean.setGs_deliver_goods_phone(tDetail[i][4]);
                        //modify 2006-02-27
                        tOrderDetailBean.setGs_deliver_goods_confirm_date(tDetail[i][5].substring(0,10));
                        //add 2006-02-27
                        tOrderDetailBean.setGs_deliver_goods_confirm_time(tDetail[i][5].substring(10));
                        //add 2006-02-27
                        tOrderDetailBean.setGs_deliver_goods_explain(tDetail[i][6]);
                        tOrderDetailBean.setGs_lastmodify_contents("接收到交易平台传来的订货明细送货确认信息");
                        tOrderDetailBean.setGs_lastmodify_date(getCurDT());
                        tOrderDetailBean.setGs_lastmodifer_id("交易平台");
//                    }
                    tList.add(tOrderDetailBean);
                }else{
                    GSLogWriter.error(this,"未查到订单明细编码：" + tDetail[i][0] + "所对应的订单明细信息");
                    throw new GSException(GSException.E10040,GSException.E10040_MSG);
                }
            }
            boolean tFlag = false;
            for(int i=0; i<tList.size();i++){
                GSOrderDetailBean tOrderDetailBean = (GSOrderDetailBean)tList.get(i);
                tFlag = tOrderDetailDAO.updateOrderDetailStatus(tOrderDetailBean,tConn,GSBaseConstants.ORDER_ORDERING);
                if(tFlag){
                    continue;
                }else{
                    GSLogWriter.debug(this,"修改订单明细状态为送货确认失败");
                    tRs = "0";
                    tConn.rollback();
                    break;
                }
            }
            if(tFlag){
                tConn.commit();
                tRs = "1";
            }
        }catch(GSException ge){
            throw ge;
            
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E00001,GSException.E00001_MSG);
        }finally{
            try{
                tConn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
        return tRs;
    }
    /**
     * 处理交易平台发送的订单明细送货拒绝信息
     * @return
     * @throws GSException
     */
    private String processRefuseOrder() throws GSException{
        String tRs = "";
        Connection tConn = null;
        try{
            String tStr[] = iContent.split("&",3);
            int tCount = 0;
            //检查订单明细汇总信息格式
            if(tStr[1].substring(0,tStr[1].indexOf("=")).equalsIgnoreCase("Count")){
                tCount = Integer.parseInt(tStr[1].substring(tStr[1].indexOf("=") + 1));
            }else{
                GSLogWriter.error(this,"传入内容不包含    送货拒绝数量字段");
                throw new GSException(GSException.E10052,GSException.E10052_MSG);
            }
            String[] temp = tStr[2].split("&");
            if(temp.length % tCount != 0){
                GSLogWriter.error(this,"传入内容明细字段有缺失或多余");
                throw new GSException(GSException.E10052,GSException.E10052_MSG);
            }        
            String[][] tDetail = new String[tCount][5];
            if(temp.length / tCount != 5){
                GSLogWriter.error(this,"传入内容明细字段有缺失或多余");
                throw new GSException(GSException.E10052,GSException.E10052_MSG);
            }
            
            //检查传入明细内容格式是否正确
            int tOrderCount = 0;
            for(int i=0; i< tCount; i++){
                int j = i * 5;
                if(!temp[j].substring(0,temp[j].indexOf("_")).equalsIgnoreCase("CusOrderItemCode")){
                    GSLogWriter.error(this,"   集中采购系统订单明细ID错误");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }else{
                    tDetail[i][0] = temp[j].substring(temp[j].indexOf("=") + 1);
                    tOrderCount = tOrderCount + 1;
                }
//                if(!temp[j + 1].substring(0,temp[j + 1].indexOf("_")).equalsIgnoreCase("CusItemCode")){
//                    GSLogWriter.error(this,"  产品编号");
//                    throw new GSException(GSException.BME10052,GSException.BME10052_MSG);
//                }
                if(!temp[j + 1].substring(0,temp[j + 1].indexOf("_")).equalsIgnoreCase("ProductID")){
                    GSLogWriter.error(this,"  产品编号");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }
                if(!temp[j + 2].substring(0,temp[j + 2].indexOf("_")).equalsIgnoreCase("RefuseReason")){
                    GSLogWriter.error(this,"     拒绝原因");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }else{
                    tDetail[i][1] = temp[j + 2].substring(temp[j + 2].indexOf("=") + 1);
                }
                if(!temp[j + 3].substring(0,temp[j + 3].indexOf("_")).equalsIgnoreCase("RefuseTime")){
                    GSLogWriter.error(this,"        拒绝日期和时间");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }else{
                    tDetail[i][2] = temp[j + 3].substring(temp[j + 3].indexOf("=") + 1);
                }
                if(!temp[j + 4].substring(0,temp[j + 4].indexOf("_")).equalsIgnoreCase("CusCode")){
                    GSLogWriter.error(this,"           巴士物资编号");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }
            }
            if(tCount != tOrderCount){
                GSLogWriter.error(this,"订单明细送货拒绝数量和订单明细实际的数量不相符");
                throw new GSException(GSException.E10052,GSException.E10052_MSG);
            }
            GSOrderDetailDAO tOrderDetailDAO = new GSOrderDetailDAO();
            tConn = tOrderDetailDAO.getConnetion();
            tConn.setAutoCommit(false);
//            int tDetailCount = 0;
            ArrayList tList = new ArrayList();
            for(int i=0; i<tDetail.length; i++){
                //根据传入的每条订单明细送货拒绝信息的订单编号查询订单明细
                GSOrderDetailBean tOrderDetailBean = tOrderDetailDAO.queryOrderDetailByDetailId(tConn,tDetail[i][0]);
//                GSOrderDetailBean tOrderDetailBean = new GSOrderDetailBean();
                if(tOrderDetailBean != null){
//                    tDetailCount = tDetailCount + tOrderDetailBean.getGs_order_count();
                    tOrderDetailBean.setGs_reject_cause(tDetail[i][1]);
                    //modify 2006-02-27
                    tOrderDetailBean.setGs_reject_date(tDetail[i][2].substring(0,10));
                    //add 2006-02-27
                    tOrderDetailBean.setGs_reject_time(tDetail[i][2].substring(10));
                    //add 2006-02-27
                    tOrderDetailBean.setGs_order_detail_status(GSBaseConstants.ORDER_ORDER_REJECT);
                    tOrderDetailBean.setGs_lastmodifer_id("交易平台");
                    tOrderDetailBean.setGs_lastmodify_contents("接收到交易平台传来的订货明细送货拒绝信息");
                    tOrderDetailBean.setGs_lastmodify_date(getCurDT());
                    tList.add(tOrderDetailBean);
                }else{
                    GSLogWriter.error(this,"未查到订单明细编码：" + tDetail[i][0] + "所对应的订单明细信息");
                    throw new GSException(GSException.E10040,GSException.E10040_MSG);
                }
            }
                
            //修改订单明细状态
            boolean tFlag = false;
            for(int i=0; i < tList.size(); i++){
                GSOrderDetailBean tOrderDetailBean = (GSOrderDetailBean)tList.get(i);
                tFlag = tOrderDetailDAO.updateOrderDetailStatus(tOrderDetailBean,tConn,GSBaseConstants.ORDER_ORDERING);
                if(tFlag){
                    continue;
                }else{
                    GSLogWriter.debug(this,"修改订单明细状态为送货拒绝失败");
                    tRs = "0";
                    tConn.rollback();
                    break;
                }
            }
            if(tFlag){
                tConn.commit();
                tRs = "1";
            }
            
        }catch(GSException ge){
            throw ge;
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E00001,GSException.E00001_MSG);
        }finally{
            try{
                tConn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }        
        return tRs;
    }
    /**
     * 处理交易平台发送的订单明细交货确认信息
     * @return
     * @throws GSException
     */
    private String processFinishOrder() throws GSException{
        String tRs = "";
        Connection tConn = null;
        try{
            String tStr[] = iContent.split("&",4);
            int tCount = 0;
            //检查订单明细汇总信息格式
            if(tStr[1].substring(0,tStr[1].indexOf("=")).equalsIgnoreCase("Count")){
                tCount = Integer.parseInt(tStr[1].substring(tStr[1].indexOf("=") + 1));
            }else{
                GSLogWriter.error(this,"传入内容不包含订单明细数量字段");
                throw new GSException(GSException.E10052,GSException.E10052_MSG);
            }
            double tTotal = 0;
            if(tStr[2].substring(0,tStr[2].indexOf("=")).equals("Total")){
                tTotal = Double.parseDouble(tStr[2].substring(tStr[1].indexOf("=") + 1));
            }else{
                GSLogWriter.error(this,"传入内容不包含   产品签收数量汇总字段");
                throw new GSException(GSException.E10052,GSException.E10052_MSG);
            }
            
            GSLogWriter.info("----------------------------------------------");
            String[] temp = tStr[3].split("&");
            for(int i=0;i<temp.length;i++){
                GSLogWriter.info(temp[i]+ ", ");
            }
            GSLogWriter.info("----------------------------------------------");
            if(temp.length % tCount != 0){
                GSLogWriter.error(this,"传入内容明细字段有缺失或多余 temp.length % tCount != 0");
                throw new GSException(GSException.E10052,GSException.E10052_MSG);
            }        
            if(temp.length / tCount != 9){  //8个字段改成9个字段
                GSLogWriter.error(this,"传入内容明细字段有缺失或多余 temp.length / tCount != 9");
                throw new GSException(GSException.E10052,GSException.E10052_MSG);
            }
            String[][] tDetail = new String[tCount][9];
            //检查传入明细内容格式是否正确
            int tOrderCount = 0;
            for(int i=0; i< tCount; i++){
                int j = i * 9;              //8个字段改成9个字段
                if(!temp[j].substring(0,temp[j].indexOf("_")).equalsIgnoreCase("CusOrderItemCode")){
                    GSLogWriter.error(this,"   集中采购系统订单明细ID错误");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }else{
                    tDetail[i][0] = temp[j].substring(temp[j].indexOf("=") + 1);
                    tOrderCount = tOrderCount + 1;
                }
                if(!temp[j + 1].substring(0,temp[j + 1].indexOf("_")).equalsIgnoreCase("ProductID")){
                    GSLogWriter.error(this,"  产品编号错误");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }
                //新增送货单号字段2006-6-23
                if(!temp[j + 2].substring(0,temp[j + 2].indexOf("_")).equalsIgnoreCase("SendCode")){
                    GSLogWriter.error(this,"  送货单号错误");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }else{
                    tDetail[i][6] = temp[j+2].substring(temp[j+2].indexOf("=") + 1);
                }
                
                //成交价格
                if(!temp[j + 3].substring(0,temp[j+3].indexOf("_")).equalsIgnoreCase("SurePrice")){
                    GSLogWriter.error(this,"  成交价格错误");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }else{
                    tDetail[i][5] = temp[j + 3].substring(temp[j + 3].indexOf("=") + 1);
                }
                if(!temp[j + 4].substring(0,temp[j + 4].indexOf("_")).equalsIgnoreCase("ReceiveTime")){
                    GSLogWriter.error(this,"     签收日期和时间错误");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }else{
                    tDetail[i][1] = temp[j + 4].substring(temp[j + 4].indexOf("=") + 1);
                }
                if(!temp[j + 5].substring(0,temp[j + 5].indexOf("_")).equalsIgnoreCase("ReceiveQty")){
                    GSLogWriter.error(this,"        签收数量错误");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }else{
                    tDetail[i][2] = temp[j + 5].substring(temp[j + 5].indexOf("=") + 1);
                }
                if(!temp[j + 6].substring(0,temp[j + 6].indexOf("_")).equalsIgnoreCase("ReceivePerson")){
                    GSLogWriter.error(this,"        签收人错误");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }else{
                    tDetail[i][3] = temp[j + 6].substring(temp[j + 6].indexOf("=") + 1);
                }
                if(!temp[j + 7].substring(0,temp[j + 7].indexOf("_")).equalsIgnoreCase("ReceiveRemark")){
                    GSLogWriter.error(this,"        签收说明错误");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }else{
                    tDetail[i][4] = temp[j + 7].substring(temp[j + 7].indexOf("=") + 1);
                }
                if(!temp[j + 8].substring(0,temp[j + 8].indexOf("_")).equalsIgnoreCase("CusCode")){
                    GSLogWriter.error(this,"        巴士物资编号错误");
                    throw new GSException(GSException.E10052,GSException.E10052_MSG);
                }                
            }
            if(tCount != tOrderCount){
                GSLogWriter.error(this,"订单明细交货确认数量和订单明细实际的数量不相符");
                throw new GSException(GSException.E10052,GSException.E10052_MSG);
            }
            GSOrderDetailDAO tOrderDetailDAO = new GSOrderDetailDAO();
            tConn = tOrderDetailDAO.getConnetion();
            tConn.setAutoCommit(false);
            double tDetailCount = 0;
            ArrayList tList = new ArrayList();
            for(int i=0; i<tDetail.length; i++){
                //根据传入的每条订单明细交货确认信息的订单编号查询订单明细
                GSOrderDetailBean tOrderDetailBean = tOrderDetailDAO.queryOrderDetailByDetailId(tConn,tDetail[i][0]);
                if(tOrderDetailBean != null){
                    tDetailCount = tDetailCount + tOrderDetailBean.getGs_signin_count();
                    if(Math.abs(tOrderDetailBean.getGs_order_count() - Double.parseDouble(tDetail[i][2])) > 0.002){
                        GSLogWriter.error(this,"配送数量不等于签收数量");
//                        throw new GSException(GSException.BME10052,GSException.BME10052_MSG);
                    }
                    //modify 2006-02-27
                    tOrderDetailBean.setGs_signin_date(tDetail[i][1].substring(0,10));
                    //add 2006-02-27
                    tOrderDetailBean.setGs_signin_time(tDetail[i][1].substring(10));
                    //add 2006-02-27
                    tOrderDetailBean.setGs_signin_count(Double.parseDouble(tDetail[i][2]));
                    tOrderDetailBean.setGs_signin_person(tDetail[i][3]);
                    tOrderDetailBean.setGs_signin_explain(tDetail[i][4]);
                    tOrderDetailBean.setGs_goods_unitprice(Double.parseDouble(tDetail[i][5]));
                    tOrderDetailBean.setGs_order_detail_status(GSBaseConstants.ORDER_DELIVERY_CONFIRM);
                    tOrderDetailBean.setGs_lastmodify_contents("接收到交易平台传来的交货确认明细信息");
                    tOrderDetailBean.setGs_lastmodifer_id("交易平台");
                    tOrderDetailBean.setGs_lastmodify_date(getCurDT());
                    //新增送货单号 2006-06-23
                    tOrderDetailBean.setGs_deliver_goods_id(tDetail[i][6]);
                    tList.add(tOrderDetailBean);
                }else{
                    GSLogWriter.error(this,"未查到订单明细编码：" + tDetail[i][0] + "所对应的订单明细信息");
                    throw new GSException(GSException.E10040,GSException.E10040_MSG);
                }
            }            
            //修改订单明细状态
            boolean tFlag = false;
            for(int i=0; i < tList.size(); i++){
                GSOrderDetailBean tOrderDetailBean = (GSOrderDetailBean)tList.get(i);
                tFlag = tOrderDetailDAO.updateOrderDetailStatus(tOrderDetailBean,tConn,GSBaseConstants.ORDER_ORDER_CONFIRM);
                if(tFlag){
                    continue;
                }else{
                    GSLogWriter.debug(this,"修改订单明细状态为交货确认失败");
                    tRs = "0";
                    tConn.rollback();
                    break;
                }
            }
            if(tFlag){
                tConn.commit();
                tRs = "1";
            }
        }catch(GSException ge){
            throw ge;
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E00001,GSException.E00001_MSG);
        }finally{
            try{
                tConn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }        
        return tRs;
    }
    
    //处理交易平台对申请取消的处理
    private String processDisposeCancelOrder() throws GSException
    {
        String tRs = "";
        Connection tConn = null;
        try
        {
            GSOrderDetailDAO tOrderDetailDAO = new GSOrderDetailDAO();
            tConn = tOrderDetailDAO.getConnetion();
            tConn.setAutoCommit(false);
            ArrayList tList = new ArrayList();
            
    		ParameterFactory pf = new ParameterFactory(iContent);
    		for (int i = 0; i < Integer.parseInt(pf.getString("Count")); i++)
    		{
    			GSOrderDetailBean tOrderDetailBean = tOrderDetailDAO.queryOrderDetailByDetailId(tConn,pf.getString("CusOrderItemCode_"+i));
                if(tOrderDetailBean != null)
                {
                	tOrderDetailBean.setDisposeCancelReason(pf.getString("DisposeCancelReason_"+i));
                	tOrderDetailBean.setDisposeCancelPerson(pf.getString("DisposeCancelPerson_"+i));
                	tOrderDetailBean.setDisposeCancelTime(pf.getString("DisposeCancelTime_"+i));
                	//如果取消申请经交易平台处理同意，则集中采购系统标记为作废，否则为已确认
                	if (pf.getString("DisposeCancelStatus_"+i).equalsIgnoreCase("Y"))
                		tOrderDetailBean.setGs_order_detail_status(GSBaseConstants.ORDER_BLANK_OUT);
                	else
                		tOrderDetailBean.setGs_order_detail_status(GSBaseConstants.ORDER_ORDER_CONFIRM);
                	
                    tOrderDetailBean.setGs_lastmodify_contents("接收到交易平台传来的取消申请的处理");
                    tOrderDetailBean.setGs_lastmodifer_id("交易平台");
                    tOrderDetailBean.setGs_lastmodify_date(getCurDT());

                    tList.add(tOrderDetailBean);
                }
                else
                {
                    GSLogWriter.error(this,"未查到订单明细编码：" + pf.getString("CusOrderItemCode_"+i) + "所对应的订单明细信息");
                    throw new GSException(GSException.E10040,GSException.E10040_MSG);
                }
    		}
    		
            //修改订单明细状态
            boolean tFlag = false;
            for(int i=0; i < tList.size(); i++)
            {
                GSOrderDetailBean tOrderDetailBean = (GSOrderDetailBean)tList.get(i);
                tFlag = tOrderDetailDAO.updateOrderDetailStatus(tOrderDetailBean,tConn,GSBaseConstants.ORDER_ORDER_CONFIRM);
                if(tFlag)
                {
                    continue;
                }
                else
                {
                    GSLogWriter.debug(this,"修改订单明细状态为交货确认失败");
                    tRs = "0";
                    tConn.rollback();
                    break;
                }
            }
            if(tFlag)
            {
                tConn.commit();
                tRs = "1";
            }
        }
        catch(GSException ge)
        {
            throw ge;
        }
        catch(Exception e)
        {
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E00001,GSException.E00001_MSG);
        }
        finally
        {
            try
            {
                tConn.close();
            }
            catch(SQLException se)
            {
                se.printStackTrace();
            }
        }
        return tRs;
    }    
    
 
 
	//处理交易平台对申请作废的处理 add by Sindy 2007-10-29
	private String processDeposeOrder() throws GSException
	{
		String tRs = "";
		Connection tConn = null;
		try
		{
			GSOrderDetailDAO tOrderDetailDAO = new GSOrderDetailDAO();
			tConn = tOrderDetailDAO.getConnetion();
			tConn.setAutoCommit(false);
			ArrayList tList = new ArrayList();
            
			ParameterFactory pf = new ParameterFactory(iContent);
			for (int i = 0; i < Integer.parseInt(pf.getString("Count")); i++)
			{
				GSOrderDetailBean tOrderDetailBean = tOrderDetailDAO.queryOrderDetailByDetailId(tConn,pf.getString("CusOrderItemCode_"+i));
				if(tOrderDetailBean != null)
				{
					//tOrderDetailBean.setGs_reject_cause(pf.getString("BlankoutReason_"+i));
					//tOrderDetailBean.setGs_reject_date(pf.getString("BlankoutDate_"+i));
					//tOrderDetailBean.setGs_reject_time(pf.getString("BlankoutTime_"+i));
					//如果取消申请经交易平台处理同意，则集中采购系统标记为作废，否则为已确认
					//if (pf.getString("BlankoutStatus_"+i).equalsIgnoreCase("Y"))
					tOrderDetailBean.setGs_order_detail_status(GSBaseConstants.ORDER_BLANK_OUT);
					//else
					//	tOrderDetailBean.setGs_order_detail_status(GSBaseConstants.ORDER_ORDER_CONFIRM);
                	
					tOrderDetailBean.setGs_lastmodify_contents(pf.getString("DeposeReason_"+i));
					tOrderDetailBean.setGs_lastmodifer_id(pf.getString("DeposeUser_"+i));
					tOrderDetailBean.setGs_lastmodify_date(pf.getString("DeposeTime_"+i));

					tList.add(tOrderDetailBean);
				}
				else
				{
					GSLogWriter.error(this,"未查到订单明细编码：" + pf.getString("CusOrderItemCode_"+i) + "所对应的订单明细信息");
					throw new GSException(GSException.E10040,GSException.E10040_MSG);
				}
			}
    		
			//修改订单明细状态
			boolean tFlag = false;
			for(int i=0; i < tList.size(); i++)
			{
				GSOrderDetailBean tOrderDetailBean = (GSOrderDetailBean)tList.get(i);
				tFlag = tOrderDetailDAO.updateOrderDetailStatus(tOrderDetailBean,tConn,GSBaseConstants.ORDER_BLANK_OUT);
				if(tFlag)
				{
					continue;
				}
				else
				{
					GSLogWriter.debug(this,"修改订单明细状态为作废失败");
					tRs = "0";
					tConn.rollback();
					break;
				}
			}
			if(tFlag)
			{
				tConn.commit();
				tRs = "1";
			}
		}
		catch(GSException ge)
		{
			throw ge;
		}
		catch(Exception e)
		{
			GSLogWriter.error(this,e.toString());
			throw new GSException(GSException.E00001,GSException.E00001_MSG);
		}
		finally
		{
			try
			{
				tConn.close();
			}
			catch(SQLException se)
			{
				se.printStackTrace();
			}
		}
		return tRs;
	}     
    
    
    
    /**
     * 取得系统当前日期
     * @return
     */
    private String getCurDT(){
        String tCurDT = "";
        try{
            tCurDT = DateUtil.formateDateTime(DateUtil.getCurrentTime("DT","AD"));
        }catch(Exception e){
            e.printStackTrace();
        }
        return tCurDT;
    }
    public static void main(String arg[]){
        
        String iContent = "12345609";
        String instr = "abcd&EnDppp";            
        if(instr != null){                
            System.out.println( "client input is :" + instr);
            int preLen =0;
            if (iContent == null){
                preLen = 0;
                iContent = instr;
            }
            else{
                preLen = iContent.length();
                iContent = iContent + instr;
            }
            int idx = instr.toUpperCase().indexOf("&END");                
            if (idx > -1){
                idx = preLen + idx;
                System.out.println(iContent.substring(0, idx));
                if (iContent.length()>idx +4){
                    iContent = iContent.substring(idx + 4);  //跳过"&END"
                }
                else{
                    iContent = "";
                }
            }
            else if (iContent.length() > 16777216){  // 字符串超长, 清空之
                iContent = "";
            }
            
            System.out.println(iContent);
        }

        
        
//        try{
//            GSMultiThreadServerSoket tSokcet = new GSMultiThreadServerSoket(10000);
////            tSokcet.iContent = "Action=RefuseOrder&Count=1&CusOrderItemCode_0=10000004&ProductID_0=19010003011&RefuseReason_0=拒绝原因,该产品已停产。&RefuseTime_0=&CusCode_0=BSWZ&";
//            String[] tStr = new String[3];
//            tStr[0] = "Action=SendOrder&Count=1&Total=500.0&CusOrderItemCode_0=10000004&ProductID_0=19010003011&SendCode_0=20060303155500&SendQty_0=500.0&SendTime_0=2006-03-03&SendPerson_0=me&SendTel_0=45839291&SendCreateTime_0=2006-02-21 12:30:30&SendRemark_0=没有啊&CusCode_0=BSWZ&";          //此处拼要传的字符串
//            tStr[1] = "Action=RefuseOrder&Count=1&CusOrderItemCode_0=10000004&ProductID_0=19010003011&RefuseReason_0=拒绝原因,该产品已停产,是否有拒绝时间。&RefuseTime_0=2006-03-03&CusCode_0=BSWZ&";         //此处拼要传的字符串
//            for(int i=0; i<tStr.length;i++){
//                tSokcet.iContent = tStr[i];
//                tSokcet.process();
//            }
//            
//        }catch(Exception e){
//            e.printStackTrace();
//            
//        }
    }
}
